<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\AdminPower;
use App\Models\AdminRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class Adtor extends Base
{

    //管理员添加
    public function registerAdmin(Request $request){
        $data = $request->post();
        $rules = [
            'username'=>'required|unique:admins|string',
            'password'=>'required|alpha_num|confirmed',
            'password_confirmation'=>'required|same:password',
            'roleid'=>'required|numeric'
        ];
        $messages = [
            'username.required'=> '请填写账号',
            'username.unique'=> '该账号已存在',
            'password.required'=> '请填写密码',
            'roleid.required'=> '请选择角色',
            'password.alpha_num'=> '密码必须由数字和字母组成',
            'password.confirmed'=> '请填写确认密码',
            'password_confirmation.required'=> '请填写确认密码',
            'password_confirmation.same'=> '两次密码不一致'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $data['password'] = Hash::make($data['password']);
        unset($data['password_confirmation']);
        $data['token'] = $this->setToken($data);
        $model = new Admin();
        $result = $model->adminAdd($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'registerAdmin');
            return response()->json([
                'code'=>20002,
                'message'=>'管理员添加失败,请重试'
            ]);
        }else{
            return response()->json([
                'code'=>0,
                'message'=>'管理员添加成功'
            ]);
        }
    }

    //角色添加接口
    public function roleCreate(Request $request){
        $data = $request->post();
        $rules = [
            'rolename'=>'required|string',
            'powers'=>'required|string'
        ];
        $messages = [
            'rolename.required'=> '请填角色名称',
            'powers.required'=> '请选择权限'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $model =  new  AdminRole();
        $result = $model->roleAdd($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'powerCreate');
            return response()->json([
                'code'=>20041,
                'message'=>'角色添加失败,请检查后重试'
            ]);
        }else{
            return response()->json([
                'code'=>0,
                'message'=>'角色添加成功'
            ]);
        }
    }

    //获取角色列表
    public function roleList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $where = [];
        if($searchCont){
            $where[] = ['rolename','like','%'.$searchCont.'%'];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $data = (new AdminRole())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($data);
    }

    //角色编辑
    public function roleUpdate(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'rolename'=>'required|string',
            'powers'=>'required|string'
        ];

        $messages = [
            'id.required'=> '请选择角色',
            'rolename.required'=> '请填角色名称',
            'powers.required'=> '请选择权限'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $model =  new  AdminRole();
        $result = $model->doUpdate($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'editAdminPower');
            return response()->json([
                'code'=>20031,
                'message'=>'权限编辑失败,请重试'
            ]);
        }else{
            return response()->json($result);
        }
    }

    //获取所有菜单及其权限列表
    public function navPowers(Request $request){
        $result = (new AdminPower())->getNavPowers();
        return response()->json($result);
    }

    //根据角色ID获取其所有权限ID
    public function rolePowers(Request $request){
        $roleid = $request->get('roleid');
        $result = (new AdminPower())->getRolePowers($roleid);
        return response()->json($result);
    }

    //管理员列表
    public function admList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $where = [];
        if($searchCont){
            $where[] = ['username','like','%'.$searchCont.'%'];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $data = (new Admin())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($data);
    }

    //管理员编辑
    public function adminUpdate(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'username'=>'required|unique:admins|string',
            'roleid'=>'required|numeric'
        ];

        $messages = [
            'id.required'=> '请选择管理员',
            'username.required'=> '请填写账号',
            'username.unique'=> '该账号已存在',
            'password.required'=> '请填写密码',
            'roleid.required'=> '请选择角色'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $model =  new  Admin();
        $result = $model->doUpdate($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'adminUpdate');
            return response()->json([
                'code'=>20031,
                'message'=>'管理员编辑失败,请重试'
            ]);
        }else{
            return response()->json($result);
        }
    }

    //管理员密码重置
    public function resetAdmin(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'password'=>'required|alpha_num|confirmed',
            'password_confirmation'=>'required|same:password',
        ];
        $messages = [
            'id.required'=> '请选择管理员',
            'password.alpha_num'=> '密码必须由数字和字母组成',
            'password.confirmed'=> '请填写确认密码',
            'password_confirmation.required'=> '请填写确认密码',
            'password_confirmation.same'=> '两次密码不一致'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $data['password'] = Hash::make($data['password']);
        unset($data['password_confirmation']);
        $data['token'] = $this->setToken($data);
        $model = new Admin();
        $result = $model->adminReset($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'resetAdmin');
            return response()->json([
                'code'=>20002,
                'message'=>'管理员添加失败,请重试'
            ]);
        }else{
            return response()->json($result);
        }
    }

    //改变后台管理员状态
    public function changeAdmin(Request $request){
        $model = new Admin();
        $result = $model->adminStatus($request->post());
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'resetAdmin');
            return response()->json([
                'code'=>20002,
                'message'=>'管理员添加失败,请重试'
            ]);
        }else{
            return response()->json($result);
        }
    }
}
